package com.linwen.dataStructure.list.arrayList;

import java.util.Arrays;

/*
 * 数组类，包含了数组的增、删、查、 改
 * tset
 * */

public class MyArrayList {
	
	private int[] array;
	private int indexs;
	//无参构造方法
	public MyArrayList() {
		array = new int[3];
		indexs = -1;
	}
	//有参构造方法
	public MyArrayList(int size) {
		array = new int[size];
		indexs = -1;
	}
	//直接增加  添加在数组最后面
	public void insert(int value) {
		//判断是否需要扩容
		if(isFull()) {
			array = Arrays.copyOf(array, array.length*2);
		}
		//先自增，再添加
		array[++indexs] = value;
	}
	//根据元素，返回首个符合元素的下标
	public int search(int vaul) {
		int i;
		for(i=0;i<=indexs;i++) {
			if(vaul==array[i]) {
				return i+1;
				}
			}
		return -1;
		
	}
	//根据下标，返回元素
	public int getIndex(int index) {
		if(index<0||index>indexs) {
			throw new ArrayIndexOutOfBoundsException();	
		}else {
			return array[index-1];
		}
	}
	//根据下标，删除数据
	public void delete(int index) {
		if(index<=0||index>indexs) {
			throw new ArrayIndexOutOfBoundsException();	
		}else {
			for(int i=index;i<=indexs;i++) {
				array[i-1] = array[i];
			}
			indexs--;
		}
	}
	//根据下标更新数据
	public void upData(int index,int value) {
		if(index<=0||index>indexs) {
			throw new ArrayIndexOutOfBoundsException();
		}else {
			array[index-1] = value;
		}
	}
	//输出数组
	public void display() {
		for(int i=0;i<=indexs;i++) {
			System.out.print(array[i]+" ");
		}
	}
	//是否为空
	public boolean isEmpty() {
		return array.length == 0;
	}
	//是否满数组
	public boolean isFull() {
		return indexs == array.length-1;
	}
}
